/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.berlin.jsf.validation;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
import ru.ssau.berlin.framework.AttributeTypeEnum;

/**
 *
 * @author Caesar
 */
public abstract class AbstactValidator {

    private static final DateFormat DATE_FORMAT =
            new SimpleDateFormat("dd.MM.yyyy HH:mm");

    protected void validate(AttributeTypeEnum type, String value) {
        switch (type) {
            case BOOLEAN:
                if (!value.equalsIgnoreCase("true") &&
                        !value.equalsIgnoreCase("false")) {
                    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "True or False required",
                            "True or False required, instead of " + value));
                }
                break;
            case DATETIME:
                try {

                    DATE_FORMAT.parse(value);
                } catch (ParseException ex) {
                    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Date format required",
                            "Date format required: " + value));
                }
                break;
            case FLOAT:
                try {
                    Double.parseDouble(value);
                } catch (NumberFormatException ex) {
                    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Float Number required",
                            "Float Number required: " + value));
                }
                break;
            case INTEGER:
                try {
                    Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Integer Number required",
                            "Integer Number required: " + value));
                }
                break;
            case STRING:
                break;
            default:
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Unexpected attribute type",
                        "Unexpected attribute type: " + type));
        }

    }

}
